package cn.line5.login;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.line5.module.ModuleDao;
import cn.line5.module.ModuleFunctionPointVo;
import cn.line5.organization.OrganizationConstants;
import cn.line5.organization.OrganizationDao;
import cn.line5.organization.PermissionVo;
import cn.line5.organization.UserVo;
import cn.line5.seam.annotations.In;
import cn.line5.seam.annotations.Name;

@Name("loginService")
/**
 * <h1>登录业务层实现类</h1>
 * 
 * @author Liu,Xiaojun
 */
public class LoginServiceImpl implements LoginService {
	@In("organizationDao")
	private OrganizationDao organizationDao;

	@In("moduleDao")
	private ModuleDao moduleDao;
	
	/**
	 * 登录
	 */
	public LoginVo login(LoginVo vo){
		UserVo uVo = organizationDao.getUserByLoginName(vo.getLoginName());
		//if(uVo != null && uVo.getLoginPassword().equals(vo.getLoginPassword()) && OrganizationConstants.USER_TYPE_NORMAL.equals(uVo.getStatus())){
			vo.setUserId(uVo.getUserId());
			vo.setOrganizationId(uVo.getOrganizationId());
			vo.setRealName(uVo.getRealName());
			vo.setModuleAndFunctionPermission(getModuleAndFunctionPermission(uVo.getUserId()));
			vo.setPointPermission(getPointPermission(uVo.getUserId()));
			return vo;
		//}
		//return null;
	}

	/**
	 * 获取模块和功能权限
	 */
	public Map<String, PermissionVo> getModuleAndFunctionPermission(
			String userId) {
		Map<String, PermissionVo> map = new HashMap<String, PermissionVo>();
		PermissionVo _vo = new PermissionVo();
		_vo.setUserId(userId);
		_vo.setPermissionType(OrganizationConstants.PERMISSION_TYPE_MODULE);
		List<PermissionVo> moduleList = organizationDao.getPermissionList(_vo);
		if (moduleList != null && moduleList.size() > 0) {
			for (PermissionVo vo : moduleList)
				map.put(vo.getPermissionId(), vo);
			_vo
					.setPermissionType(OrganizationConstants.PERMISSION_TYPE_FUNCTION);
			List<PermissionVo> functionList = organizationDao
					.getPermissionList(_vo);
			if (functionList != null && functionList.size() > 0)
				for (PermissionVo vo : functionList)
					map.put(vo.getPermissionId(), vo);
		}
		return map;
	}

	/**
	 * 获取没有的功能点权限
	 */
	public Map<String, ModuleFunctionPointVo> getPointPermission(String userId) {
		Map<String, ModuleFunctionPointVo> map = new HashMap<String, ModuleFunctionPointVo>();
		List<ModuleFunctionPointVo> list = moduleDao.getPointList();
		PermissionVo _vo = new PermissionVo();
		_vo.setUserId(userId);
		_vo.setPermissionType(OrganizationConstants.PERMISSION_TYPE_POINT);
		List<PermissionVo> pointList = organizationDao.getPermissionList(_vo);
		if (list != null && list.size() > 0) {
			for (ModuleFunctionPointVo vo : list) {
				if (pointList != null && pointList.size() > 0) {
					Boolean t = true;
					for (PermissionVo pVo : pointList) {
						if (pVo.getPermissionId().equals(
								String.valueOf(vo.getPointId()))) {
							pointList.remove(pVo);
							t = false;
							break;
						}
					}
					if (t)
						map.put(vo.getComponentName() + "."
								+ vo.getMethodName(), vo);
				} else {
					map.put(vo.getComponentName() + "." + vo.getMethodName(),
							vo);
				}
			}
		}
		return map;
	}

}
